package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.New.HotdetailsActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LogisticsinquircarouselmapBean;
import com.example.myapplication.ShuJu.Re_News;
import com.example.myapplication.ShuJu.Re_NewsListbean;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyHolder>{
    private Context context;
    private List<Re_NewsListbean.RowsBean> list;


    public NewsAdapter(Context context, List<Re_NewsListbean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }




    @NonNull
    @Override
    public NewsAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,null);
        return new NewsAdapter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsAdapter.MyHolder holder, int position) {
        Glide.with(context).load("http://124.93.196.45:10001/"+list.get(position).getCover()).into(holder.cover);
        holder.title.setText(list.get(position).getTitle());
        holder.contentNum.setText(String.valueOf(list.get(position).getCommentNum()));
        String content = list.get(position).getContent();
        if (content != null && list.get(position).getContent().length() >=15) {
            holder.content.setText(Html.fromHtml(list.get(position).getContent().substring(0,15)+"..."));
        } else {
            holder.content.setText(Html.fromHtml(list.get(position).getContent()));
        }
        holder.pubDate.setText(list.get(position).getCreateTime());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HotdetailsActivity.class);
                intent.putExtra("listID",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView cover;
        TextView title;
        TextView contentNum;
        TextView content;
        RelativeLayout rootView;
        TextView pubDate;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.cover);
            rootView = itemView.findViewById(R.id.rootView);
            content = itemView.findViewById(R.id.content);
            title = itemView.findViewById(R.id.title);
            contentNum = itemView.findViewById(R.id.contentNum);
            pubDate = itemView.findViewById(R.id.pubDate);
        }
    }
}

